from fastapi import HTTPException
from typing import Any, Dict, Optional


class VideoProcessingError(Exception):
    """视频处理相关错误"""
    pass


class FileUploadError(Exception):
    """文件上传错误"""
    pass


class TranscriptionError(Exception):
    """语音转录错误"""
    pass


class VideoGenerationError(Exception):
    """视频生成错误"""
    pass


class TemplateNotFoundError(Exception):
    """模���未找到错误"""
    pass


def setup_exception_handlers(app):
    """设置异常处理器"""
    from fastapi import Request
    from fastapi.responses import JSONResponse
    import logging

    logger = logging.getLogger(__name__)

    @app.exception_handler(VideoProcessingError)
    async def video_processing_exception_handler(request: Request, exc: VideoProcessingError):
        logger.error(f"Video processing error: {exc}")
        return JSONResponse(
            status_code=422,
            content={
                "error": "视频处理错误",
                "message": str(exc)
            }
        )

    @app.exception_handler(FileUploadError)
    async def file_upload_exception_handler(request: Request, exc: FileUploadError):
        logger.error(f"File upload error: {exc}")
        return JSONResponse(
            status_code=400,
            content={
                "error": "文件上传错误",
                "message": str(exc)
            }
        )

    @app.exception_handler(TranscriptionError)
    async def transcription_exception_handler(request: Request, exc: TranscriptionError):
        logger.error(f"Transcription error: {exc}")
        return JSONResponse(
            status_code=422,
            content={
                "error": "语音转录错误",
                "message": str(exc)
            }
        )

    @app.exception_handler(VideoGenerationError)
    async def video_generation_exception_handler(request: Request, exc: VideoGenerationError):
        logger.error(f"Video generation error: {exc}")
        return JSONResponse(
            status_code=422,
            content={
                "error": "视频生成错误",
                "message": str(exc)
            }
        )

    @app.exception_handler(TemplateNotFoundError)
    async def template_not_found_exception_handler(request: Request, exc: TemplateNotFoundError):
        logger.error(f"Template not found: {exc}")
        return JSONResponse(
            status_code=404,
            content={
                "error": "模板未找到",
                "message": str(exc)
            }
        )